package com.watt.gateway.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 */
@Slf4j
public class PropertiesUtil {
    private static PropertiesUtil util = null;
    private static Map<String, Properties> props = null;

    private PropertiesUtil() {

    }

    public static PropertiesUtil getInstance() {
        if (util == null) {
            props = new HashMap<String, Properties>();
            util = new PropertiesUtil();
        }
        return util;
    }

    public Properties load(String name) {
        if (props.get(name) != null) {
            return props.get(name);
        } else {
            Properties prop = new Properties();
            try {
                prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(name + ".properties"));
                props.put(name, prop);
                return prop;
            } catch (IOException e) {
                log.error("", e);
            }
        }
        return null;
    }

    /**
     * @param propertiesnName 属性文件名
     * @return
     */
    public Properties getPropertiesByName(String propertiesnName) {
        Properties properties = this.load(propertiesnName);
        return properties;
    }

    public static void main(String[] args) {
        System.out.println(PropertiesUtil.class.getResourceAsStream(""));
    }
}

